function varargout = iCreateAreaTypes(varargin)
% ICREATEAREATYPES M-file for iCreateAreaTypes.fig
%      ICREATEAREATYPES, by itself, creates a new ICREATEAREATYPES or raises the existing
%      singleton*.
%
%      H = ICREATEAREATYPES returns the handle to a new ICREATEAREATYPES or the handle to
%      the existing singleton*.
%
%      ICREATEAREATYPES('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ICREATEAREATYPES.M with the given input arguments.
%
%      ICREATEAREATYPES('Property','Value',...) creates a new ICREATEAREATYPES or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before iCreateAreaTypes_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to iCreateAreaTypes_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help iCreateAreaTypes

% Last Modified by GUIDE v2.5 12-Mar-2012 12:02:45

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @iCreateAreaTypes_OpeningFcn, ...
                   'gui_OutputFcn',  @iCreateAreaTypes_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT






function RemoveItem(handles)
global ncurrentitem;

global strtypes;
global bFemales;
global bAssignGenders;

ncurrentitem = get(handles.listboxstrings, 'Value');

if ncurrentitem > 0
    strtypes(ncurrentitem) = [];
    bFemales(ncurrentitem) = [];
    bAssignGenders(ncurrentitem) = [];
    
    UpdateListBox(handles)
end



function AddItem(handles)

global strtypes;
global bFemales;
global bAssignGenders;

itemstr = get(handles.editadd, 'String');

strtypes = [strtypes, {itemstr}];
bFemales = [bFemales, -1];
bAssignGenders = [bAssignGenders, 0];

UpdateListBox(handles)
set(handles.editadd, 'String', '');



function UpdateListBox(handles)
global strtypes;

%len = length(strtypes);
      
set(handles.listboxstrings, 'Value', 1);
set(handles.listboxstrings, 'String', strtypes);        



function UpdateRadioButtons(handles)

global ncurrentitem;
global bFemales;
global bAssignGenders;

ncurrentitem = get(handles.listboxstrings, 'Value');

if ncurrentitem > 0
    
    set(handles.radiobuttonmale, 'Value', 0);
    set(handles.radiobuttonfemale, 'Value', 0);    
    set(handles.radiobuttonnogender, 'Value', 0);
        
    if bFemales(ncurrentitem) == 0
        set(handles.radiobuttonmale, 'Value', 1);
    elseif bFemales(ncurrentitem) == 1
        set(handles.radiobuttonfemale, 'Value', 1);
    else
        set(handles.radiobuttonnogender, 'Value', 1);
    end
    
    if bAssignGenders(ncurrentitem) == 1
        set(handles.checkboxassign, 'Value', 1);
    else
        set(handles.checkboxassign, 'Value', 0);
    end
        
end


function OpenDataFile(handles)

global savefile;
global savefilefull;
global pathname;

[filename,pathname] = uigetfile({'*.mat','Data Files (*.mat)';'*.*',  'All Files (*.*)'},'Pick a file');

if filename ~= 0    
    savefile = filename;
    savefilefull = strcat(pathname, filename);    
    
    set(handles.editdata, 'String', filename);
    
    LoadDataFile(handles)
end


function LoadDataFile(handles)

global strtypes;
global bFemales;
global bAssignGenders;

global savefilefull;

if ~isempty(savefilefull)
    scheck = dir(savefilefull);
    
    if ~isempty(scheck)
        load(savefilefull);         
        
        UpdateListBox(handles);
        UpdateRadioButtons(handles);
    end
end


function SaveData(handles)


global strtypes;
global bFemales;
global bAssignGenders;

global pathname;

sfile = get(handles.editdata, 'String');

if pathname == 0
    pathname = '';
end

sfile = strcat(pathname, sfile);


if ~isempty(sfile) & ~isempty(strtypes)
    save(sfile, 'strtypes', 'bFemales', 'bAssignGenders');
    
    set(handles.textoutput, 'String', 'File successfully saved.');
else                 
    if isempty(savefilefull)
        set(handles.textoutput, 'String', 'No file name given - file not saved.');
    end
    
    if isempty(strtypes)
        set(handles.textoutput, 'String', 'No items in list to save - file not saved.');
    end
end



function RadioButtonsClicked(handles)

global bFemales;

ncurrentitem = get(handles.listboxstrings, 'Value');

if ncurrentitem > 0
    
    bmale = get(handles.radiobuttonmale, 'Value');
    bfemale = get(handles.radiobuttonfemale, 'Value');    
    bnogender = get(handles.radiobuttonnogender, 'Value');
      
    if bmale == 1 & bfemale == 0 & bnogender == 0        
        bFemales(ncurrentitem) = 0;
    elseif bmale == 0 & bfemale == 1 & bnogender == 0        
        bFemales(ncurrentitem) = 1;
    elseif bmale == 0 & bfemale == 0 & bnogender == 1       
        bFemales(ncurrentitem) = -1;
    end
        
end


function CheckBoxClicked(handles)

global bAssignGenders;

ncurrentitem = get(handles.listboxstrings, 'Value');

if ncurrentitem > 0

  bAssignGenders(ncurrentitem) = get(handles.checkboxassign, 'Value');
   
end





% --- Executes just before iCreateAreaTypes is made visible.
function iCreateAreaTypes_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to iCreateAreaTypes (see VARARGIN)

% Choose default command line output for iCreateAreaTypes
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes iCreateAreaTypes wait for user response (see UIRESUME)
% uiwait(handles.figure1);

global strtypes;
global bFemales;
global bAssignGenders;

global pathname;

strtypes = {};
bFemales = [];
bAssignGenders = [];

pathname = '';


% --- Outputs from this function are returned to the command line.
function varargout = iCreateAreaTypes_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbuttonadd.
function pushbuttonadd_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonadd (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


AddItem(handles);


% --- Executes on selection change in listboxstrings.
function listboxstrings_Callback(hObject, eventdata, handles)
% hObject    handle to listboxstrings (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listboxstrings contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxstrings

UpdateRadioButtons(handles);


% --- Executes during object creation, after setting all properties.
function listboxstrings_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listboxstrings (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttonremove.
function pushbuttonremove_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonremove (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

RemoveItem(handles)


function editadd_Callback(hObject, eventdata, handles)
% hObject    handle to editadd (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editadd as text
%        str2double(get(hObject,'String')) returns contents of editadd as a double


% --- Executes during object creation, after setting all properties.
function editadd_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editadd (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttonsave.
function pushbuttonsave_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonsave (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


SaveData(handles);




function editdata_Callback(hObject, eventdata, handles)
% hObject    handle to editdata (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editdata as text
%        str2double(get(hObject,'String')) returns contents of editdata as a double


% --- Executes during object creation, after setting all properties.
function editdata_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editdata (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in checkboxassign.
function checkboxassign_Callback(hObject, eventdata, handles)
% hObject    handle to checkboxassign (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkboxassign

CheckBoxClicked(handles);


% --- Executes on button press in pushbuttonopen.
function pushbuttonopen_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonopen (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

OpenDataFile(handles);


% --- Executes when selected object is changed in uipanel1.
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel1 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)

RadioButtonsClicked(handles);


% --- Executes on button press in pushbuttoncancel.
function pushbuttoncancel_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttoncancel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

close();
